package com.eteng.demo.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPClient;

public class SaveFtpFile implements Runnable{

	FTPClient client;
	String path = "";
	ArrayList<String> fileNames;
	
	/**
	 * 
	 * @param is 内容来源
	 * @param path 文件的保存地址
	 * @param fileName 保存文件的命名
	 */
	public SaveFtpFile ( ArrayList<String> fileName, String path, FTPClient client) {
		this.path = path;
		this.client = client;
		this.fileNames = fileName;
	}
	
	@Override
	public void run() {
		
		for(String fileName: fileNames){
			System.out.println("写文件");
			File localFile = new File(path + fileName); 
			OutputStream os = null;
			try{
				if(!localFile.exists()){
					localFile.createNewFile();
				}else{
					System.out.println("文件已存在");
					return;
				}
				os = new FileOutputStream(localFile);  
				if(client.retrieveFile(fileName, os)){
					client.completePendingCommand();
				}
				os.close();
				
			} catch (Exception e){
				
			}
		}		
	}
		
}

